home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / checkedm.jav < prev    next >
Encoding:
Text File  |  1995-12-14  |  8.4 KB  |  326 lines

  1. /*
  2.   File: CheckedMap.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   24Sep95  dl@cs.oswego.edu   Create from collections.java  working file
  11.   13Oct95  dl                 Misc clean-up
  12.  
  13. */
  14.   
  15. package collections;
  16.  
  17. import java.util.Enumeration;
  18. import java.util.NoSuchElementException;
  19.  
  20. /**
  21.  *
  22.  *
  23.  * CheckedMap supports standard update operations on maps.
  24.  *
  25.  * @author Doug Lea
  26.  * @version 0.93
  27.  *
  28.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  29. **/
  30.  
  31.  
  32. public class CheckedMap extends CheckedCollection implements UpdatableMap {
  33.  
  34. /**
  35.  * Wrap Map m inside a checker
  36. **/
  37.   public CheckedMap(UpdatableMap m) { super(m); }
  38.  
  39. /**
  40.  * Make a Checked clone of underlying collection
  41. **/
  42.   protected Object clone() throws CloneNotSupportedException  { 
  43.     return new CheckedMap((UpdatableMap)(thys.duplicate()));
  44.   }
  45.  
  46. /**
  47.  * return casted version of CheckedCollection.thys.
  48. **/
  49.   public UpdatableMap thys() { return (UpdatableMap)thys; }
  50.  
  51. /**
  52.  * return casted version of CheckedCollection.prev.
  53. **/
  54.  
  55.   public UpdatableMap prev() { return (UpdatableMap)prev; }
  56.  
  57.  
  58. /**
  59.  * Checks collections.Map.canIncludeKey according to its specification
  60.  * @see collections.Map#canIncludeKey
  61. **/
  62.   public boolean canIncludeKey(Object key) {
  63.     preCheck(); 
  64.     boolean result = thys().canIncludeKey(key);
  65.     assert(!(result == true && key == null));
  66.     assert(thys.sameStructure(prev));
  67.     postCheck();
  68.     return result;
  69.   }
  70.  
  71.  
  72. /**
  73.  * Checks collections.Map.includesKey according to its specification
  74.  * @see collections.Map#includesKey
  75. **/
  76.   public synchronized boolean     includesKey(Object key) {
  77.     preCheck(); 
  78.     boolean result = thys().includesKey(key);
  79.     assert(!(result == true && !thys().canInclude(key)));
  80.     assert(thys.sameStructure(prev));
  81.     postCheck();
  82.     return result;
  83.   }
  84.  
  85. /**
  86.  * Checks collections.Map.includesAt according to its specification
  87.  * @see collections.Map#includesAt
  88. **/
  89.   public synchronized boolean     includesAt(Object key, Object value) {
  90.     preCheck(); 
  91.     boolean result = thys().includesAt(key, value);
  92.     assert(!(result == true && (!thys().includesKey(key) || !thys().includes(value))));
  93.     assert(thys.sameStructure(prev));
  94.     postCheck();
  95.     return result;
  96.   }
  97.  
  98.  
  99. /**
  100.  * Checks collections.Map.keys according to its specification
  101.  * @see collections.Map#keys
  102. **/
  103.   public synchronized CollectionEnumeration keys() {
  104.     int c = thys().size();
  105.     CollectionEnumeration k = keys();
  106.     while (k.hasMoreElements()) {
  107.       assert(k.numberOfRemainingElements() == c);
  108.       --c;
  109.       Object key = k.nextElement();
  110.       Object v = thys().at(k);
  111.       assert(thys().canIncludeKey(key));
  112.       assert(thys().canInclude(v));
  113.       assert(thys().aKeyOf(v) != null);
  114.       assert(thys().includesKey(key));
  115.       assert(thys().includes(v));
  116.       assert(thys().includesAt(k, v));
  117.     }
  118.  
  119.     postCheck(); 
  120.     // return a new one
  121.     return thys().keys();
  122.   }
  123.  
  124.  
  125. /**
  126.  * Checks collections.Map.at according to its specification
  127.  * @see collections.Map#at
  128. **/
  129.   public synchronized Object      at(Object key) 
  130.   throws  NoSuchElementException {
  131.     preCheck(); 
  132.     try {
  133.       Object result = thys().at(key);
  134.       assert(thys().includesAt(key, result));
  135.       assert(thys.sameStructure(prev));
  136.       postCheck();
  137.       return result;
  138.     }
  139.     catch (NoSuchElementException ex) {
  140.       assert(!thys().includesKey(key));
  141.       assert(thys.sameStructure(prev));
  142.       postCheck();
  143.       throw ex;
  144.     }
  145.  
  146.   }
  147.  
  148. /**
  149.  * Checks collections.Map.aKeyOf according to its specification
  150.  * @see collections.Map#aKeyOf
  151. **/
  152.   public synchronized Object aKeyOf(Object element) {
  153.     preCheck(); 
  154.     Object result = thys().aKeyOf(element);
  155.     assert((result == null && !thys().includes(element)) ||
  156.       (thys().at(result).equals(element)));
  157.     assert(thys.sameStructure(prev));
  158.     postCheck();
  159.     return result;
  160.   }
  161.  
  162.  
  163.  
  164. /**
  165.  * Checks collections.UpdatableMap.putAt according to its specification
  166.  * @see collections.UpdatableMap#putAt
  167. **/
  168.   public synchronized void putAt(Object key, Object element) 
  169.   throws IllegalElementException {
  170.     preCheck();
  171.     try {
  172.       thys().putAt(key, element);
  173.       checkPut(thys(), prev(), key, element, true);
  174.       postCheck();
  175.     }
  176.     catch (IllegalElementException ex) {
  177.       assert(!canIncludeKey(key) || !canInclude(element));
  178.       assert(thys.sameStructure(prev));
  179.       postCheck();
  180.       throw ex;
  181.     }
  182.   }
  183.  
  184. /**
  185.  * Checks collections.Map.puttingAt.
  186.  * @see collections.Map#puttingAt
  187. **/
  188.   public synchronized  Map  puttingAt(Object key, Object element) 
  189.   throws IllegalElementException {
  190.     preCheck();
  191.     try {
  192.       Map nc = thys().puttingAt(key, element);
  193.       checkPut(nc, thys(), key, element, false);
  194.       assert(thys.sameStructure(prev));
  195.       postCheck();
  196.       return nc;
  197.     }
  198.     catch (IllegalElementException ex) {
  199.       assert(!canIncludeKey(key) || !canInclude(element));
  200.       assert(thys.sameStructure(prev));
  201.       postCheck();
  202.       throw ex;
  203.     }
  204.  
  205.   }
  206.  
  207.  
  208.  
  209. /**
  210.  * Checks collections.UpdatableMap.removeAt according to its specification
  211.  * @see collections.UpdatableMap#removeAt
  212. **/
  213.   public synchronized void removeAt(Object key) {
  214.     preCheck();
  215.     thys().removeAt(key);
  216.     checkRemoveAt(thys(), prev(), key, true);
  217.     postCheck();
  218.   }
  219.  
  220.  
  221. /**
  222.  * Checks collections.Map.removingAt
  223.  * @see collections.Map#removingAt
  224. **/
  225.   public synchronized  Map   removingAt(Object key) {
  226.     preCheck();
  227.     Map nc = thys().removingAt(key);
  228.     checkRemoveAt(nc, thys(), key, false);
  229.     assert(thys.sameStructure(prev));
  230.     postCheck();
  231.     return nc;
  232.   }
  233.  
  234.  
  235. /**
  236.  * Checks collections.UpdatableMap.replaceElement according to its specification
  237.  * @see collections.UpdatableMap#replaceElement
  238. **/
  239.   public synchronized void replaceElement(Object key, Object oldElement, 
  240.                                           Object newElement) 
  241.   throws IllegalElementException {
  242.     preCheck();
  243.     try {
  244.       thys().replaceElement(key, oldElement, newElement);
  245.       assert((!thys().includesAt(key, oldElement) ||
  246.          thys().includesAt(key, newElement)));
  247.       assert(thys().size() == prev().size());
  248.       assert(!(thys().version() == prevVersion && 
  249.           prev().includesAt(key, oldElement)));
  250.       CollectionEnumeration prevKeys = prev().keys();
  251.       while (prevKeys.hasMoreElements()) {
  252.         Object k = prevKeys.nextElement();
  253.         assert((k.equals(key) || thys().includesAt(k, prev().at(k))));
  254.       }
  255.       postCheck();
  256.     }
  257.     catch (IllegalElementException ex) {
  258.       assert(!canInclude(newElement));
  259.       assert(thys.sameStructure(prev));
  260.       postCheck();
  261.       throw ex;
  262.     }
  263.   }
  264.  
  265.  
  266. /**
  267.  * Helper for checking put*
  268. **/
  269.  
  270.   protected void checkPut(Map nc, Map oc, Object key,
  271.                           Object element, boolean verchk) {
  272.  
  273.     assert(nc.canIncludeKey(key));
  274.     assert(nc.canInclude(element));
  275.  
  276.     int reqSize = oc.size();
  277.     if (!oc.includesKey(key)) reqSize = reqSize + 1;
  278.     assert(nc.includesAt(key, element));
  279.     assert(nc.size() == reqSize);
  280.  
  281.     CollectionEnumeration os = oc.keys();
  282.     while (os.hasMoreElements()) {
  283.       Object k = os.nextElement();
  284.       assert((k.equals(key) || nc.includesAt(k, oc.at(k))));
  285.     }
  286.  
  287.     if (verchk) {
  288.       int nv = ((UpdatableCollection)nc).version();
  289.       assert((nv == prevVersion) == oc.includesAt(key, element));
  290.     }
  291.     nc.checkImplementation();
  292.  
  293.   }
  294.  
  295. /**
  296.  * Helper for checking remov*
  297. **/
  298.  
  299.   protected void checkRemoveAt(Map nc, Map oc, Object key,
  300.                                boolean verchk) {
  301.  
  302.     int reqSize = oc.size();
  303.     if (oc.includesKey(key)) reqSize = reqSize - 1;
  304.     assert(!nc.includesKey(key));
  305.     assert(nc.size() == reqSize);
  306.  
  307.     CollectionEnumeration os = oc.keys();
  308.     while (os.hasMoreElements()) {
  309.       Object k = os.nextElement();
  310.       assert((k.equals(key) || nc.includesAt(k, oc.at(k))));
  311.     }
  312.  
  313.     if (verchk) {
  314.       int nv = ((UpdatableCollection)nc).version();
  315.       assert((nv == prevVersion) == !oc.includesKey(key));
  316.     }
  317.  
  318.     nc.checkImplementation();
  319.  
  320.   }
  321.  
  322.  
  323. };
  324.  
  325.  
  326.